package rs.frenjoynet.core.core;

import common.db.repository.Resourcer;

import javax.ejb.Stateful;

import rs.frenjoynet.core.core.entity.db.Information;
import rs.frenjoynet.core.core.entity.db.User;
import rs.frenjoynet.core.core.ActiveUserSession;
import rs.frenjoynet.core.core.util.ResourceProperties;
import rs.frenjoynet.core.core.db.DBBroker;
import rs.frenjoynet.core.core.db.MySqlHibDBBrokerImpl;
import rs.frenjoynet.core.core.cache.UserCache;
import cz.atria.common.lang.RandomGUID;
import cz.atria.common.lang.Util;

import java.util.List;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 16.10.2009.
 * Time: 12.46.13
 * To change this template use File | Settings | File Templates.
 */
@Stateful
public class CoreSecurityFacadeBean extends BaseFacadeImpl implements CoreSecurityFacade
{
	private DBBroker dbBroker = new MySqlHibDBBrokerImpl();
	private static final String SQL_PATH = "rs/frenjoynet/core/core/sql";
	private Resourcer resourcer;
	private ResourceProperties resourceProperties = new ResourceProperties("master_admin.properties");

	private Log log = LogFactory.getLog(this.getClass());

	@Override
	public ActiveUserSession autentificate(String login, String password, String webSessionUid, String userAgent,
										   String ip)
	{
		//User user = getUsersCache().getUserByEmail(login);
		LoginKind loginKind = LoginKind.Customer;
		log.debug(login);
		if (resourceProperties.getKey("username").contains(login))
		{
			String passKey = resourceProperties.getKey(login);
			log.debug("passKey " + passKey);
			if (!Util.isEmpty(passKey))
			{
				if (Util.toMD5Hash(password).equals(passKey))
					loginKind = LoginKind.Master_Administrator;
			}
			else
				return null;

		}

		String guid = RandomGUID.generateJoint();


		ActiveUserSession activeSession = new ActiveUserSession();
//		activeSession.setBeginDate((Date)result[1]);
//		activeSession.setLastPingDate((Date)result[1]);
//		activeSession.setId((String)result[0]);
//		activeSession.setRowId((String)result[2]);
		activeSession.setIp(ip);
		activeSession.setUserAgent(userAgent);
//		activeSession.setLanguageId(user.getLanguageId());
		activeSession.setEmail(login);
		activeSession.setWebSessionUid(webSessionUid);
		activeSession.setGuid(guid);
		activeSession.setLoginKind(loginKind);

		Information inf = new Information();
		inf.setFirstName("Firstname["+login+"]");
		inf.setLastName("lastname["+password+"]");
		activeSession.setInformation(inf);

		setActiveSession(activeSession);

		return activeSession;
	}

	@Override
	public ActiveUserSession createAccount(String login, String password, String webSessionUid, String userAgent,
										   String ip, Information information)
	{
		return null;
	}

	@Override
	public void logout()
	{

	}

	@Override
	public ActiveUserSession getActiveUserSession()
	{
		return getActiveSession();
	}

	@Override
	public User getUser(String username)
	{
		String sql = getResourcer().query("getUserByUsername");
		return (User)dbBroker.query(User.class, sql, username);

	}

	@Override
	public Information getInformation(String username)
	{
		return null;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public void initialize()
	{
		super.initialize();
		// should execute only once during app startup
		if (!isStaticInitialized(CoreSecurityFacadeBean.class))
		{
			loadUserCache();

		}
	}

	private void setActiveSession(ActiveUserSession activeSession)
	{
		getActiveUserSessionCache().put(activeSession);
		setSessionGuid(activeSession.getGuid());
	}

	private void loadUserCache()
	{
		String sql = getResourcer().query("GetUsers");

		List<User> users = dbBroker.query(User.class, sql);
		getUsersCache().clear();
		getUsersCache().put(users);
	}

	public Resourcer getResourcer()
	{
		if (resourcer == null)
		{
			resourcer = new Resourcer(SQL_PATH);
		}
		return resourcer;
	}

	private UserCache getUsersCache()
	{
		return new UserCache(getUtilisCache());
	}
}
